package hai.wom.org.qyer_destination;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import hai.wom.org.qyer_destination.bean.HotCity;
import hai.wom.org.qyer_destination.callback.ICallback;
import hai.wom.org.qyer_destination.callback.IList;
import hai.wom.org.qyer_destination.fragment.Fragment_City;
import hai.wom.org.qyer_destination.fragment.Fragment_Hot;
import hai.wom.org.qyer_destination.utils.HttpUtils;

@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
    private String path = "http://open.qyer.com/lastminute/conf/destination?client_id=qyer_discount_androi&client_secret=227097da1d07a2a9860f&track_user_id=&track_deviceid=352284042226724&track_app_version=2.0.4&track_app_channel=qq&track_device_info=hlte&track_os=Android4.4.2&app_installtime=1483531797801&size=720x1242&ra_referer=app_home&lat=39.084881&lon=117.199403";
    private FragmentManager manager;
    //
    public List<HotCity.DataEntity>datas;
    private IList cbhot,cbcity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        x.view().inject(this);
//        setContentView(R.layout.activity_main);
        manager = getSupportFragmentManager();
        loadData();
        init();
    }

    private void init() {
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment_City fc=new Fragment_City();
        cbcity=fc;
        Fragment_Hot fh=new Fragment_Hot();
        cbhot=fh;
        fh.setICB(fc);
        transaction.replace(R.id.fl_fg_city,fc);
        transaction.replace(R.id.fl_fg_hot, fh);
        transaction.commit();
        datas=new ArrayList<>();
    }
    private void loadData() {
        new HttpUtils(new ICallback() {
            @Override
            public void getData(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    JSONArray array = jsonObject.getJSONArray("data");
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject js = array.getJSONObject(i);
                        HotCity.DataEntity data = new HotCity.DataEntity();
                        data.setName(js.getString("name"));

                        JSONArray destinations = js.getJSONArray("destinations");
                        List<HotCity.DataEntity.DestinationsEntity> des_list = new ArrayList<HotCity.DataEntity.DestinationsEntity>();
                        for (int i1 = 0; i1 < destinations.length(); i1++) {
                            JSONObject js_content = destinations.getJSONObject(i1);
                            HotCity.DataEntity.DestinationsEntity des = new HotCity.DataEntity.DestinationsEntity();
                            des.setName(js_content.getString("name"));
                            des.setName_en(js_content.getString("name_en"));
                            des.setPic(js_content.getString("pic"));
                            des_list.add(des);
                        }
                        data.setDestinations(des_list);
                        datas.add(data);
                    }
                    cbcity.sendData(datas);
                    cbhot.sendData(datas);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).execute(path);
    }
}
